#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dlgsernew.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    this->m_ltreeMenu = new QMenu;
    ui->setupUi(this);
    this->initMainWindow();
}

//bool MainWindow::eventFilter(QObject *obj, QEvent *event)
//{
//  if (event->type() == QEvent::MouseButtonPress)
//  {
//    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
//    Qt::MouseButtons *button;
//    statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
//    if(mouseEvent->button()==Qt::RightButton){

//    }
//  }
//  return false;
//}

MainWindow::~MainWindow()
{
    delete ui;
    delete m_ltreeMenu;
    delete m_actAddSer;
    delete m_actDelSer;
}

void MainWindow::on_action_Save_triggered()
{

}


/*****************Bengin Server stats functions******************/
void MainWindow::showTreeContextMenu(const QPoint& point)
{
    this->m_ltreeMenu->exec(this->ui->leftTView->viewport()->mapToGlobal(point));
}

void MainWindow::on_actAddser_triggered()
{
    this->ui->button_test->setText(tr("action works"));
    CDlgSerNew *pDlgSerNew = new CDlgSerNew;
    pDlgSerNew->move(QCursor::pos());
    pDlgSerNew->show();
}

void MainWindow::on_actDelser_triggered()
{
    this->ui->button_test->setText(tr("del works"));

}

void MainWindow::initMainWindow(){
    /*Set up left tree view*/
    QTreeView *tv=this->ui->leftTView;
    QStandardItemModel *standardModel = new QStandardItemModel();
    QStandardItem *rootNode = standardModel->invisibleRootItem();
    standardModel->setHorizontalHeaderItem(0, new QStandardItem("Servers"));
    //defining a couple of items
    QStandardItem *mexicoItem =  new QStandardItem("Canada");
    //register the model
    tv->setModel(standardModel);
    tv->expandAll();

    this->m_actAddSer = new QAction(tr("Add Server"), tv);
    connect(this->m_actAddSer, SIGNAL(triggered()), this, SLOT(on_actAddser_triggered()));
    this->m_actDelSer = new QAction(tr("Del Server"), tv);
    connect(this->m_actDelSer, SIGNAL(triggered()), this, SLOT(on_actDelser_triggered()));

    this->m_ltreeMenu->addAction(m_actAddSer);
    this->m_ltreeMenu->addAction(m_actDelSer);

    connect(tv, SIGNAL(customContextMenuRequested(const QPoint&)),
            this, SLOT(showTreeContextMenu(const QPoint&)));
    tv->setContextMenuPolicy(Qt::CustomContextMenu);
    /* End left tree view setup */

    connect(this->ui->btnServers,SIGNAL(pressed()), SLOT(on_otherThanDevButton()));
    //connect(this->ui->btnDev,SIGNAL(pressed()), SLOT(this->ui->toolBox->setVisible(true)));


    this->ui->toolBox->setVisible(false);




    //selection changes shall trigger a slot
   // QItemSelectionModel *selectionModel= tv->selectionModel();


//    connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
//            this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)))

    // set context menu policy

    // initialize the model with three empty nodes

    // create an add action and connect it to a signal
}

void MainWindow::on_btnServers_pressed()
{
    this->ui->leftTView->setVisible(true);

}


void MainWindow::on_btnDev_pressed()
{
    this->ui->leftTView->setVisible(false);
    this->ui->toolBox->setVisible(true);

}

void MainWindow::on_otherThanDevButton()
{    this->ui->toolBox->setVisible(false);
}
